package org.istt.web;

import org.apache.commons.lang.math.NumberUtils;
import org.istt.domain.FileStatus;
import org.istt.service.FileStatusManager;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 *
 */
public class FileStatusAction extends BaseAction<FileStatusManager, FileStatus, Long> {
	private String fileStatusId;
	private String fileStatusName;
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	
	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("fileStatus.saved"));
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("fileStatus.deleted"));
	}

	@Override
	public Long getId() {
		Long id = NumberUtils.toLong(fileStatusId);
		if (id == 0) return null;
		return id;
	}

	/**
	 * @param fileStatusId the fileStatusId to set
	 */
	public void setFileStatusId(String fileStatusId) {
		this.fileStatusId = fileStatusId;
	}

	/**
	 * @return the fileStatusId
	 */
	public String getFileStatusId() {
		return fileStatusId;
	}

	/**
	 * @param fileStatusName the fileStatusName to set
	 */
	public void setFileStatusName(String fileStatusName) {
		this.fileStatusName = fileStatusName;
	}

	/**
	 * @return the fileStatusName
	 */
	public String getFileStatusName() {
		if (entity!=null) {
			return entity.getStatus();
		}
		return fileStatusName;
	}
	
	@Override
	public FileStatus getEntity() {
		FileStatus c = new FileStatus();
		c.setId(getId());
		c.setStatus(getFileStatusName());
		return c;
	}
	
	@Override
	public void validate() {
		FileStatus result = ((FileStatusManager)mgr).searchIgnoreCaseByStatus(getFileStatusName());
		
		if (getId() == null) {// create new entity
			if (result != null) {
				addActionError(getText("fileStatus.name.exist"));
			}
		} else {// update entity
			if (result != null) {
				if (!getId().equals(result.getId())) {
					addActionError(getText("fileStatus.name.exist"));
				}
			}
		}
	}
	
}
